import { Component, OnInit } from '@angular/core';
import { NzMessageService, UploadFile, NzModalService } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { FormBuilder, Validators } from '@angular/forms';
import { urlService } from './../../../core/api/url';
import { Router, ActivatedRoute } from '@angular/router';
import { DatePipe } from '@angular/common';
import { I18nService } from 'src/app/core/i18n/i18n.service';

@Component({
	templateUrl: './userAdd.html',
	providers: [DatePipe],
})
export class UserAddComponent implements OnInit {
	validateForm;
	imageVerifyCodeId;
	e:number;
	disabled = false;
	userLevel;
	vipExpireType;
	modal;
	constructor(private i18n:I18nService ,private apiService: ApiService, private nzMessageService: NzMessageService, private fb: FormBuilder, private router: Router, private datePipe: DatePipe, private activeRouter: ActivatedRoute,private modalService: NzModalService) {
		this.i18n.get(['userLevel','vipExpireType','modal']).subscribe((res:any)=>{
			this.userLevel = res.userLevel;
			this.vipExpireType = res.vipExpireType;
			this.modal = res.modal;
		})
	}
	ngOnInit() {
		this.validateForm = this.fb.group({
			loginId: [null, [Validators.required]],
			mobile: [null, [Validators.required]],
			email: [null, [Validators.required,Validators.email]],
			loginPw: [null, [Validators.required]],
			reLoginPw: [null, [Validators.required]],
			imageVerifyCodeId: [null, [Validators.required]],
			imageCode: [null, [Validators.required]],
			verifyCode: [null, [Validators.required]],
			level: ['vip', [Validators.required]],
			vipExpireTime: [null, [Validators.required]],
			vipExpireType: [null, [Validators.required]],
		});
		this.apiService.post('生成图形验证码唯一id', {}).then(res=>{
			this.imageVerifyCodeId = res['data']['imageVerifyCodeId'];
			this.validateForm.patchValue({imageVerifyCodeId:this.imageVerifyCodeId});
		});
	}
	onChangImgCode() {
		this.e = Math.random() * 10000;
	}
	changeLevel(e){
		if(e === 'normal'){
			this.validateForm.get('vipExpireTime').setValidators([]);
			this.validateForm.get('vipExpireType').setValidators([]);
			this.validateForm.patchValue({vipExpireTime:''});
		}else if(e === 'vip'){
			this.validateForm.get('vipExpireTime').setValidators([Validators.required]);
			this.validateForm.get('vipExpireType').setValidators([Validators.required]);
		}
	}
	changeExpireType(e){
		if(e == 1){
			let date = new Date();
			date.setFullYear(date.getFullYear() + 1);
			this.validateForm.get('vipExpireTime').setValue(date);
		}else{
			this.validateForm.get('vipExpireTime').setValue(null);
		}
	}
	sendCode(e) {
		const element = e.target;
		if(this.validateForm.controls.email.valid){
			if (!this.disabled) {
				this.disabled = true;
				this.apiService
					.post('发送邮箱验证码', { email: this.validateForm.value.email, type: 'reg' })
					.then(res => {
						this.nzMessageService.success(this.modal.userAddS1);
						let t = 60;
						element.innerHTML = t + 's' + this.modal.sendCode;
						const i = setInterval(() => {
							t--;
							element.innerHTML = t + 's' + this.modal.sendCode;
							if (t < 1) {
								clearInterval(i);
								this.disabled = false;
							}
						}, 1000);
					})
			}
		}else{
			this.nzMessageService.warning(this.modal.userAddW1);
		}
	}
	submit() {
		console.log(this.validateForm.value);
		for (const v in this.validateForm.controls) {
			this.validateForm.controls[v].markAsDirty();
			this.validateForm.controls[v].updateValueAndValidity();
		}
		if (this.validateForm.valid) {
			const data = {...this.validateForm.value};
			data.vipExpireTime = data.vipExpireTime ? this.datePipe.transform(data.vipExpireTime, 'yyyy-MM-dd') : '';
			this.apiService.post('管理平台增加用户', data).then((res: any) => {
				this.modalService.confirm({
					nzTitle: this.modal.title,
					nzContent: this.modal.userAddS2,
					nzOkText: this.modal.userAddBtn1,
					nzOkType: 'primary',
					nzOnOk: () => {
						this.apiService.post('冻结/激活用户',{id:res.data.id,active:'AUDIT'}).then((res:any)=>{
							this.nzMessageService.success(this.modal.userSuccess1);
							this.router.navigateByUrl('/basicSetting/users');
						});
					},
					nzCancelText: this.modal.userAddBtn2,
					nzOnCancel: () => {
						this.router.navigateByUrl('/basicSetting/users');
					}
				  });
				
			});
		}
	}
}
